package com.auxgroup.hr.front.site.co.hireperson;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCHireChiguT;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HireChiguTService;
import com.auxgroup.hr.front.site.co.BaseCO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 档案信息CO
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hireChigu")
public class HireChiguCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(HireChiguCO.class);

	@Autowired
	public HireChiguTService hireChiguTService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	/**
	 * 获取入职人员资格证书
	 * @param rsHireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String rsHireId,BigDecimal cSeqno) {
		logger.info("getHireChigu param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"档案的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		PsCHireChiguT hireChigu = hireChiguTService.getByHire(hirePrimary);
		return buildRespone(hireChigu);
	}
	
	/**
	 * 保存档案信息
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(PsCHireChiguT hireChigu) {
		logger.info("saveHireChigu param= {}", hireChigu);
		if(StringUtils.isBlank(hireChigu.getRsHireId())){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(hireChigu.getcSeqno() != null){
			PsCHirePrimaryClass primary = new PsCHirePrimaryClass();
			primary.setcSeqno(hireChigu.getcSeqno());
			primary.setRsHireId(hireChigu.getRsHireId());
			PsCHireChiguT nowChigu = hireChiguTService.getByHire(primary);
			if(nowChigu == null){
				hireChigu = hireChiguTService.save(nowChigu);
			}else{
				objectMapper.map(hireChigu, nowChigu,"notCopyNull_hireChigu");
				hireChigu = hireChiguTService.save(nowChigu);
			}
		}else{
			hireChigu = hireChiguTService.save(hireChigu);
		}
		if(hireChigu.getcSeqno() != null){
			return buildRespone(hireChigu);
		}
		return buildRespone(false);
		
	}
	
	/**
	 * 删除档案信息
	 * @return
	 */
	@PostMapping(value = "/del")
	public BaseResponseVO del(String rsHireId,BigDecimal cSeqno) {
		logger.info("delHireChigu param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"档案的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		Boolean isdel = hireChiguTService.delete(hirePrimary);
		return buildRespone(isdel);
	}
	
	/**
	 * 获取全部资格证书
	 * @param rsHireId
	 * @return
	 */
	@GetMapping(value = "/getAll")
	public BaseResponseVO getAll(String rsHireId){
		logger.info("getAll param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		List<PsCHireChiguT> list = hireChiguTService.getAll(rsHireId);
		return buildRespone(list);
	}
}
